package com.iamk.feature;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.text.StyledEditorKit.BoldAction;

import com.iamk.util.LoadGMM;
import com.jMEF.gmm.MixtureModel;
import com.jMEF.gmm.PVector;

// Input list vetors features output r - label each images;
public class ImagesNoRelevance {
	ArrayList<float[]> featuresRegion;
	double[][] density;
	ArrayList<MixtureModel> mixArrayList;
	ArrayList<String> label;

	public ImagesNoRelevance(ArrayList features) {
		// TODO Auto-generated constructor stub
		this.featuresRegion = features;
		label = new ArrayList<String>();
		init();
	}

	// Computer desity p(xij|wm)
	private void init() {
		mixArrayList = LoadGMM.getMixtureModels();
		density = new double[featuresRegion.size()][mixArrayList.size()];
		for (int i = 0; i < featuresRegion.size(); i++) {
			for (int j = 0; j < mixArrayList.size(); j++) {
				System.out.println(featuresRegion.get(i).length);
				density[i][j] = mixArrayList.get(j).density(subArr(featuresRegion.get(i)));
			}
		}
	}

	private PVector subArr(float[] arr) {
		PVector pv = new PVector(arr.length - 1);
		double[] result = new double[arr.length - 1];
		for (int i = 0; i < arr.length - 1; i++) {
			result[i] = arr[i];
		}
		pv.array = result;
		return pv;
	}

	// Computer p(wk|Bi, Ni) ~ p(x|wk)p(Ni|wk)p(wk);
	private void densityLabel() {
		for (int i = 0; i < featuresRegion.size(); i++) {
			double valueMax = 0, temp = 0;
			String strlabel = null;
			for (int j = 0; j < mixArrayList.size(); j++) {
				// density[i][j] =
				// mixArrayList.get(j).density(subArr(featuresRegion.get(i)));
				temp = density[i][j] * mixArrayList.get(j).weighLabel;
				if (temp > valueMax) {
					valueMax = temp;
					strlabel = ""+(j + 1);
				}
			}
			// Check them label o day nua la ok
			if (!label.contains(strlabel)) {
				label.add(strlabel);
			}
		}
	}

	public ArrayList<String> getLabel() {
		densityLabel();
		return label;
	}
}
